# -*- coding: utf-8 -*-

from scpy2.conveyor_driven_worker import ConveyorDrivenWorker
import datetime
from parse_util.trans_recruitment_format import job_info_trans

class RecruitmentStorer(ConveyorDrivenWorker):

    def _process_task_msg(self, msg, params=None):
        self.logger.debug(type(msg))
        for x in msg:
            # self.logger.debug(type(x))
            # self.logger.debug(type(x[0]))
            y = x

            self.__save_to_pqsql(y)
            # self.__save_to_dynamodb(y)

    def _check_info_exist(self, _url,_releaseTime,_companyName, sqle):
        sqle.sql = "select * from recruitment where url=%s and to_char(release_time,'YYYY-MM-DD')=%s and company_name=%s"
        sqle.empty_params()
        sqle.params.append(_url)
        sqle.params.append(_releaseTime)
        sqle.params.append(_companyName)
        result = sqle.execute_dataset()
        if len(result):
            return True
        else:
            return False

    def __save_to_pqsql(self, item):
        sqle = self._components['sqlexecutor']
        item = job_info_trans(item)
        _url = item.get('sourceUrl','')
        _releaseTime = item.get('releaseTime')
        _companyName = item.get('companyName')
        try:
            if not self._check_info_exist(_url,_releaseTime,_companyName,sqle):
                _keys = [
                    'sourceUrl',
                    'companyName',
                    'categoryType',
                    'category',
                    'salary',
                    'city',
                    'workingYears',
                    'degree',
                    'jobTitle',
                    'description',
                    'source',
                    'companyUrl',
                    'entity',
                    'keywordsDes',
                    'welfare',
                    'keywords',
                    'logo',
                    'scale',
                    'requireNum',
                    'jobNature',
                    'industry',
                    'position',
                    'releaseTime',
                    'salaryText',
                    'workingYearsText',
                    'address'
                ]
                _values = [item[key] for key in _keys]
                _values.append(datetime.datetime.now())
                sqle.sql = """
                          INSERT INTO recruitment(
                                  url,
                                  company_name,
                                  category_type,
                                  category,
                                  salary,
                                  city,
                                  working_years,
                                  degree,
                                  job_title,
                                  description,
                                  source,
                                  company_url,
                                  entity,
                                  keywords_des,
                                  welfare,
                                  keywords,
                                  logo,
                                  scale,
                                  require_num,
                                  job_nature,
                                  industry,
                                  position,
                                  release_time,
                                  salary_text,
                                  working_years_text,
                                  address,
                                  update_time
                          )  VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                          """
                sqle.empty_params()
                sqle.params = _values
                sqle.execute_non_query()
            else:
                self.logger.info('recruitment info already exist')
        except Exception,e:
            self._logger.error('insert recruitment info failed for %s'%str(e))


    # def __save_to_dynamodb(self, x):
    #     table = self._components['dynamodb'].get_table(self._cfg['dynamodb_table'])
    #     table.put_item(Item={
    #         'url': x['url'],
    #         'content': x['content'][0],
    #         'label_content': x['content'][1]
    #     })


if __name__ == '__main__':
    worker = RecruitmentStorer()
    worker.run()
